﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;

using VirtualTournaments.DataAccess.SqlServer.DataMappers;

namespace VirtualTournaments.DataAccess.SqlServer.DataProxies
{
	internal sealed class ThreeWaysBetTypeDataProxy : Business.Bets.ThreeWaysBetType, IDataProxy<SqlServer.BetType>, Identifiable<SqlServer.BetType>
	{
		internal ThreeWaysBetTypeDataProxy(SqlServer.BetType dataObject, Business.Tournaments.Match match)
			: base(match)
		{
			InnerDataObject = dataObject;
			base.BetTargets = MapBetTargets(); // sin lazy loading para esta propiedad
		}

		#region Mapping methods

		private IList<Business.Bets.BetTarget> MapBetTargets()
		{
			return InnerDataObject.BetTargets.Where(betTarget => betTarget.Kind == (int)BetTargetKind.ThreeWaysBetTarget).Select(betTarget => ThreeWaysBetTargetsDataMapper.Instance.DataObject2BusinessObject(betTarget.ThreeWaysBetTarget)).Cast<Business.Bets.BetTarget>().ToList();
		}

		#endregion

		#region IDataProxy<BetType> Members

		public SqlServer.BetType InnerDataObject { get; set; }

		#endregion

		#region Identifiable<BetType> Members

		public Expression<Func<SqlServer.BetType, bool>> GetUniqueWhereExpression()
		{
			return betType => betType.Kind == (int)BetTypeKind.ThreeWaysBetType && betType.BetTypeId == InnerDataObject.BetTypeId;
		}

		#endregion
	}
}
